﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            timer1.Start();
        }

        public class RequestState
        {
            public HttpWebRequest Request;

            public RequestState()
            {
                Request = null;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
     /*     try 
            {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://hq.sinajs.cn/list=s_sh000001,s_sz399001,sh000016,sz399300");
            RequestState rs = new RequestState();

            rs.Request = req;
            req.BeginGetResponse(new AsyncCallback(this.ResponseCallback), rs);
            }
          catch (Exception exp)
          {
              while (exp != null)
              {
                  exp = exp.InnerException;
              }
          } */
           
        }
       private void ResponseCallback(IAsyncResult ar)
        {
            RequestState rs = (RequestState) ar.AsyncState;
            HttpWebRequest req = rs.Request;
            HttpWebResponse resp =  (HttpWebResponse) req.EndGetResponse(ar);
            Stream responseStream = resp.GetResponseStream();
            StreamReader sr =  new StreamReader( responseStream, Encoding.Default);
            string strContent = sr.ReadToEnd();
          //  MessageBox.Show(strContent);
            responseStream.Close();
        }

       private void timer1_Tick(object sender, EventArgs e)
       {
           WebRequest req = WebRequest.Create("http://hq.sinajs.cn/list=sh000016,sz399300");
           WebResponse res = req.GetResponse();    // GetResponse blocks until the response arrives
           Stream ReceiveStream = res.GetResponseStream();    // Read the stream into a string
           Encoding encode = Encoding.Default;
           StreamReader sr = new StreamReader(ReceiveStream, encode);
           string resultstring = "";
           while (sr.Peek() >= 0)
           {
               string value=sr.ReadLine().Split('"')[1];
               resultstring += value.Split(',')[0];
               resultstring += ": \t";
               resultstring += value.Split(',')[3];
               resultstring += ",";
               resultstring += value.Split(',')[30];
               resultstring += " ";
               resultstring += value.Split(',')[31];
               resultstring += "\r\n";
               
           }
           textBox1.Text = resultstring;
       }
    }
}